package com.sims.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.sims.dao.IStnUserDao;
import com.sims.eo.StnUser;
import com.sims.util.DataSourceUtil;
import com.sims.util.JdbcUtil;
import com.sims.util.MySQLConnBean;

public class StnUserDaoImp implements IStnUserDao {

	@Override
	public StnUser getUserbyAccount(String userAccount) {
		StnUser stnUser = null;
		MySQLConnBean mySqlBean = new MySQLConnBean();
		try {
			mySqlBean.OpenConn();
			// 调用MySqlBean中加载JDBC驱动的方法
			String strSql = "select * from stn_user where user_account='"
					+ userAccount + "'";
			ResultSet rs = mySqlBean.executeQuery(strSql);
			while (rs.next()) {
				stnUser = new StnUser();
				stnUser.setId(rs.getLong("id"));
				stnUser.setUserAccount(userAccount);
				stnUser.setUserPassword(rs.getString("user_password"));
				stnUser.setUserName(rs.getString("user_name"));
				stnUser.setCellphone(rs.getString("cellphone"));
				stnUser.setLockFlag(rs.getString("lock_flag"));
				stnUser.setActivityStatus(rs.getString("activity_status"));
				stnUser.setCreatedDate(rs.getDate("created_date"));
				stnUser.setLastUpdateDate(rs.getDate("last_update_date"));
				break;
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			mySqlBean.closeStmt();
			mySqlBean.closeConn();
		}
		return stnUser;
	}

	@Override
	public StnUser getUserbyAccount_jdbc(String userAccount) {
		QueryRunner qr = new QueryRunner();
        String sql = "select * from stn_user where user_account=?";
        
        Map<String,String> map = new HashMap<String,String>();
        map.put("user_account","userAccount");
        map.put("user_password","userPassword");
        map.put("user_name","userName");
        map.put("lock_flag","lockFlag");
        map.put("activity_status","activityStatus");
        map.put("created_date","createdDate");
        map.put("last_update_date","lastUpdateDate");
        
        Object[] params = {userAccount};
        StnUser stnUser = null;
        Connection conn = null;
		try {
			conn = JdbcUtil.getConnection();
			//Object obj = qr.query(conn, sql, new BeanHandler<StnUser>(StnUser.class), params);
			stnUser = (StnUser) qr.query(conn, sql, new BeanHandler<StnUser>(StnUser.class,new BasicRowProcessor(new BeanProcessor(map))), params);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.release(conn, null, null);
		}
		return stnUser;
	}

	@Override
	public StnUser getUserbyAccount_dataSource(String userAccount) {
		QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select * from stn_user where user_account=?";
        
        Map<String,String> map = new HashMap<String,String>();
        map.put("user_account","userAccount");
        map.put("user_password","userPassword");
        map.put("user_name","userName");
        map.put("lock_flag","lockFlag");
        map.put("activity_status","activityStatus");
        map.put("created_date","createdDate");
        map.put("last_update_date","lastUpdateDate");
       
        Object[] params = {userAccount};
        StnUser stnUser = null;
		try {
			Object obj = qr.query(sql, new BeanHandler<StnUser>(StnUser.class), params);
			
			stnUser = (StnUser) qr.query(sql, new BeanHandler<StnUser>(StnUser.class,new BasicRowProcessor(new BeanProcessor(map))), params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stnUser;
	}
}
